﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class MicroWebsiteGoods
    {
        private readonly Chain.IDAL.MicroWebsiteGoods dal = new Chain.IDAL.MicroWebsiteGoods();

        public int Add(Chain.Model.MicroWebsiteGoods model)
        {
            if (this.Exists(model.MicroGoodsCode, model.MicroGoodsShopID))
            {
                return -1;
            }
            return this.dal.Add(model);
        }

        public List<Chain.Model.MicroWebsiteGoods> DataTableToList(DataTable dt)
        {
            List<Chain.Model.MicroWebsiteGoods> list = new List<Chain.Model.MicroWebsiteGoods>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.MicroWebsiteGoods item = new Chain.Model.MicroWebsiteGoods();
                    if ((dt.Rows[i]["MicroGoodsID"] != null) && (dt.Rows[i]["MicroGoodsID"].ToString() != ""))
                    {
                        item.MicroGoodsID = int.Parse(dt.Rows[i]["MicroGoodsID"].ToString());
                    }
                    if ((dt.Rows[i]["MicroGoodsCode"] != null) && (dt.Rows[i]["MicroGoodsCode"].ToString() != ""))
                    {
                        item.MicroGoodsCode = dt.Rows[i]["MicroGoodsCode"].ToString();
                    }
                    if ((dt.Rows[i]["MicroGoodsClassID"] != null) && (dt.Rows[i]["MicroGoodsClassID"].ToString() != ""))
                    {
                        item.MicroGoodsClassID = int.Parse(dt.Rows[i]["MicroGoodsClassID"].ToString());
                    }
                    if ((dt.Rows[i]["MicroGoodsName"] != null) && (dt.Rows[i]["MicroGoodsName"].ToString() != ""))
                    {
                        item.MicroGoodsName = dt.Rows[i]["MicroGoodsName"].ToString();
                    }
                    if ((dt.Rows[i]["MicroSalePrice"] != null) && (dt.Rows[i]["MicroSalePrice"].ToString() != ""))
                    {
                        item.MicroSalePrice = decimal.Parse(dt.Rows[i]["MicroSalePrice"].ToString());
                    }
                    if ((dt.Rows[i]["MicroPrice"] != null) && (dt.Rows[i]["MicroPrice"].ToString() != ""))
                    {
                        item.MicroPrice = decimal.Parse(dt.Rows[i]["MicroPrice"].ToString());
                    }
                    if ((dt.Rows[i]["MicroPoint"] != null) && (dt.Rows[i]["MicroPoint"].ToString() != ""))
                    {
                        item.MicroPoint = int.Parse(dt.Rows[i]["MicroPoint"].ToString());
                    }
                    if ((dt.Rows[i]["MicroGoodsBidPrice"] != null) && (dt.Rows[i]["MicroGoodsBidPrice"].ToString() != ""))
                    {
                        item.MicroGoodsBidPrice = decimal.Parse(dt.Rows[i]["MicroGoodsBidPrice"].ToString());
                    }
                    if ((dt.Rows[i]["MicroGoodsRemark"] != null) && (dt.Rows[i]["MicroGoodsRemark"].ToString() != ""))
                    {
                        item.MicroGoodsRemark = dt.Rows[i]["MicroGoodsRemark"].ToString();
                    }
                    if ((dt.Rows[i]["MicroGoodsPicture"] != null) && (dt.Rows[i]["MicroGoodsPicture"].ToString() != ""))
                    {
                        item.MicroGoodsPicture = dt.Rows[i]["MicroGoodsPicture"].ToString();
                    }
                    if ((dt.Rows[i]["MicroGoodsCreateTime"] != null) && (dt.Rows[i]["MicroGoodsCreateTime"].ToString() != ""))
                    {
                        item.MicroGoodsCreateTime = DateTime.Parse(dt.Rows[i]["MicroGoodsCreateTime"].ToString());
                    }
                    if ((dt.Rows[i]["MicroGoodsShopID"] != null) && (dt.Rows[i]["MicroGoodsShopID"].ToString() != ""))
                    {
                        item.MicroGoodsShopID = int.Parse(dt.Rows[i]["MicroGoodsShopID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int MicroGoodsID)
        {
            return this.dal.Delete(MicroGoodsID);
        }

        public bool DeleteList(string MicroGoodsIDlist)
        {
            return this.dal.DeleteList(MicroGoodsIDlist);
        }

        public bool Exists(int MicroGoodsID)
        {
            return this.dal.Exists(MicroGoodsID);
        }

        public bool Exists(string strGoodsCode, int ShopID)
        {
            return this.dal.Exists(strGoodsCode, ShopID);
        }

        public bool Exists(int goodsID, string goodsCode, int ShopID)
        {
            return this.dal.Exists(goodsID, goodsCode, ShopID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListSP(PageSize, PageIndex, out resCount, strWhere);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.MicroWebsiteGoods GetModel(int MicroGoodsID)
        {
            return this.dal.GetModel(MicroGoodsID);
        }

        public List<Chain.Model.MicroWebsiteGoods> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public int Update(Chain.Model.MicroWebsiteGoods model)
        {
            if (this.Exists(model.MicroGoodsID, model.MicroGoodsCode, model.MicroGoodsShopID))
            {
                return -1;
            }
            return this.dal.Update(model);
        }
    }
}

